package se.webbzon.boltzmann.minigame.cells;

import java.awt.Color;
import java.awt.Image;

import se.webbzon.boltzmann.minigame.chemistry.AbstractMolecule;
import se.webbzon.boltzmann.minigame.chemistry.CarbonDioxideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.OxygenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SandBoxMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SugarMolecule;
import se.webbzon.boltzmann.minigame.chemistry.WaterMolecule;
import se.webbzon.boltzmann.minigame.sandbox.SandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;

public class PlantCell extends SandBoxCell {
	
	private static final double waterDrain = 1.0 / 3000.0;
	private static final double carbonDrain = 1.0 / 4000.0;
	
	// The oxygen and sugar level of this animal cell
	private final CellBar water, carbon;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public PlantCell() {
		this(1,1,1);
	}
	
	public PlantCell(double waterLevel, double carbonLevel, double lightLevel) {
		AbstractCell cell = getInstance();
		water = new CellBar(Color.blue, waterLevel);
		carbon = new CellBar(Color.lightGray, carbonLevel);
		cell.addBar(water);
		cell.addBar(carbon);
	}
	
	/*============================================================
	Protected Methods
	============================================================*/
	
	@Override protected ResourceSet<Image> getImage() {
		return Resources.getImageSet("minigame_botany_plant_cell");
	}

	@Override protected boolean isHealthy() {
		return water.getValue() > 0 && carbon.getValue() > 0;
	}

	@Override protected boolean canSplit() {
		return water.getValue() > 0.5 && carbon.getValue() > 0.5;
	}

	@Override protected void onConsume() {
		water.setValue(water.getValue() - waterDrain);
		carbon.setValue(carbon.getValue() - carbonDrain);
	}

	@Override protected boolean canReact() {
		return water.getValue() > 0.3 && carbon.getValue() > 0.3;
	}

	@Override protected void onReact() {
		water.setValue(water.getValue() - 0.3);
		carbon.setValue(carbon.getValue() - 0.3);
		SandBox box = getSandBox();
		SandBoxMolecule m;
		if (box != null) for (int i = 0; i < 6; i++) {
			m = new OxygenMolecule();
			m.setPosition(getX(), getY());
			box.addItem(m);
		}
		m = new SugarMolecule();
		m.setPosition(getX(), getY());
		box.addItem(m);
	}

	@Override protected Action consumeResource(SandBoxItem item) {
		if (item instanceof WaterMolecule) {
			water.setValue(water.getValue() + 0.2);
			return Action.Consumed;
		} else if (item instanceof CarbonDioxideMolecule) {
			carbon.setValue(carbon.getValue() + 0.2);
			return Action.Consumed;
		} else if (item instanceof SandBoxMolecule) {
			water.setValue(water.getValue() - 0.1);
			carbon.setValue(carbon.getValue() - 0.1);
			return Action.Disliked;
		} else
			return Action.Rejected;
	}

	@Override protected void onSplit() {
		SandBox box = getSandBox();
		
		// Create new cells
		PlantCell cell;
		for (int i = 0; i < 2; i++) {
			cell = new PlantCell(water.getValue() * 0.5 ,carbon.getValue() * 0.5, 1);
			double sign = i == 0 ? -1 : 1;
			cell.setPosition(getX() + sign * getWidth() * 0.4, getY());
			box.addItem(cell);
		}
		
		super.onSplit();
	}

}
